package com.xujiang.myblog.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xujiang.myblog.R;
import com.xujiang.myblog.entity.UserFragmentEntity;
import com.xujiang.myblog.entity.UserFragmentNestEntity;
import com.xujiang.myblog.entity.UserShow;
import com.xujiang.myblog.model.PreferUser;
import com.xujiang.myblog.network.retrofit.MRetrifit;
import com.xujiang.myblog.ui.LoginActivity;
import com.xujiang.myblog.util.Callback4;
import com.xujiang.myblog.util.L;
import com.xujiang.myblog.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * Created by xujiang on 2017/4/17.
 */

public class UserFragemtAdapter extends BaseMultiItemQuickAdapter<UserFragmentEntity,BaseViewHolder>
        implements BaseQuickAdapter.OnItemClickListener,BaseQuickAdapter.OnItemChildClickListener{

    private Context context;
    private NestAdapter nestAdapter;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public UserFragemtAdapter(List<UserFragmentEntity> data,Context context) {
        super(data);
        this.context = context;
        addItemType(UserFragmentEntity.CLICK_ITEM_CHILD_VIEW, R.layout.item_useroroneline_userfragment);
        addItemType(UserFragmentEntity.NEST_CLICK_ITEM_CHILD_VIEW,R.layout.item_nest_userfragment);
        addItemType(UserFragmentEntity.CLICK_ITEM_VIEW,R.layout.item_logout_userdfragment);
    }

    @Override
    protected void convert(BaseViewHolder helper, UserFragmentEntity item) {
        switch (helper.getItemViewType()){
            case UserFragmentEntity.CLICK_ITEM_CHILD_VIEW:

                helper.addOnClickListener(R.id.user_avatars_imv_userfragment);
                helper.addOnClickListener(R.id.user_rl);
                helper.addOnClickListener(R.id.ll_myblog);
                helper.addOnClickListener(R.id.ll_seemyblog);
                helper.addOnClickListener(R.id.ll_fans);
                UserShow userShow = (UserShow)item.t;

                helper.setText(R.id.tv_name_user_fragment,userShow.getName());
                helper.setText(R.id.tv_intro_user_fragment,userShow.getIntro());
                helper.setText(R.id.tv_property_user_fragment,userShow.getProperty());
                Glide.with(mContext).load(userShow.getImageurl()).bitmapTransform(new CropCircleTransformation(mContext)).into((ImageView)helper.getView(R.id.user_avatars_imv_userfragment));
                if (userShow.getImageurl1() != null) {
                    Glide.with(mContext).load(userShow.getImageurl1()).into((ImageView)helper.getView(R.id.iv1_user_fragment));
                }
                if(userShow.getImageurl2() != null){
                    Glide.with(mContext).load(userShow.getImageurl2()).into((ImageView)helper.getView(R.id.iv2_user_fragment));
                }

                helper.setText(R.id.blog_number_userFragment,userShow.getBlognumber()+"");
                helper.setText(R.id.blog_userFragment,userShow.getBlog());

                helper.setText(R.id.attentionnumber_userfragment,userShow.getAttentionnumber()+"");
                helper.setText(R.id.attention_userfragment, userShow.getAttention());

                helper.setText(R.id.fansnumber_userfragment,userShow.getFansnumber()+"");
                helper.setText(R.id.fans_userfragment,userShow.getFans());
                break;


            case UserFragmentEntity.NEST_CLICK_ITEM_CHILD_VIEW:
                helper.setNestView(R.id.ll_nest_USerFragment);
                RecyclerView recyclerView = helper.getView(R.id.RCV_nest_USerFragment);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(helper.getConvertView().getContext(), LinearLayoutManager.VERTICAL, false);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.setHasFixedSize(true);

                List<UserFragmentNestEntity> list = new ArrayList<>();
                String url1 = "http://img4.duitang.com/uploads/item/201208/08/20120808114607_yhVUJ.jpeg";
                String url2 = "http://img4.duitang.com/uploads/item/201208/08/20120808114607_yhVUJ.jpeg";
                for(int i=0;i<10;i++){
                    list.add(new UserFragmentNestEntity(url1,"item"+i,"subitem"+i,url2));
                }
                nestAdapter = new NestAdapter(R.layout.item_list_userfragment,list);
                nestAdapter.openLoadAnimation();

                //nestAdapter.setNotDoAnimationCount(8);
                nestAdapter.setOnItemClickListener(this);
                recyclerView.setAdapter(nestAdapter);
                break;

            case UserFragmentEntity.CLICK_ITEM_VIEW:
                helper.addOnClickListener(R.id.cv_logout_userfragment);
               break;
        }
    }



    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Toast.makeText(mContext, "嵌套RecycleView item 收到: " + "点击了第 " + position + " 一次", Toast.LENGTH_SHORT).show();
    }



    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Toast.makeText(mContext, "childView click", Toast.LENGTH_SHORT).show();
    }
}
